﻿using System;
using System.Collections.Generic;
using System.Text;

using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.DAL;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using HZ.Supervise.BLL;
using System.Runtime.Remoting.Messaging;

namespace HZ.Supervise.BLL
{
    public class BLL_MessageTypeDetail : IBLL_MessageTypeDetail
    {
        /// <summary>
        /// 插入规则
        /// </summary>
        /// <param name="MessageTypeDetail">model</param>
        /// <returns>0失败，1成功</returns>
        public int InsertIntoMessageTypeDetail(Model.MessageTypeDetail MessageTypeDetail)
        {
            return DAL_MessageTypeDetail.InsertIntoMessageTypeDetail(MessageTypeDetail);
        }

        /// <summary>
        /// 判断规则是否重复
        /// </summary>
        /// <param name="MessageTypeDetal">model</param>
        /// <returns>true重复 false不重复</returns>
        public bool IsExistMessageTypeDetail(Model.MessageTypeDetail MessageTypeDetal)
        {
            return DAL_MessageTypeDetail.IsExistMessageTypeDetail(MessageTypeDetal);
        }
        /// <summary>
        /// 根据条件查询表
        /// </summary>
        /// <param name="messageTypeDetail"></param>
        /// <param name="areaid"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public DataTable GetMessageTypeDetail(Model.MessageTypeDetail messageTypeDetail, bool IsWork, string type, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageTypeDetail(messageTypeDetail, IsWork, type, currentpage, pagesize, out totalrecord);
        }

        public DataTable GetMessageTypeDetailByType(Model.MessageTypeDetail messageTypeDetail, string type, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageTypeDetailByType(messageTypeDetail, type, currentpage, pagesize, out totalrecord);
        }
        /// <summary>
        /// 根据条件查询消息
        /// </summary>
        /// <param name="messageTypeDetail"></param>
        /// <param name="areaid"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public DataTable GetMessage(Model.Message message, bool IsRead, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessage(message, IsRead, currentpage, pagesize, out totalrecord);
        }
        /// <summary>
        /// 获得预警信息的详情（连接采购单表包括采购信息）
        /// </summary>
        /// <param name="id">预警信息编号</param>
        /// <param name="bllID">采购单编号</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页最大数</param>
        /// <param name="totalrecord"></param>
        /// <returns>预警信息详情（包括采购单信息）</returns>
        public DataTable GetMessageOrderDetail(string id, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageOrderDetail(id, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 获得预警信息的详情（连接采购单表包括采购信息）
        /// </summary>
        /// <param name="id">预警信息编号</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页最大数</param>
        /// <param name="totalrecord"></param>
        /// <returns>预警信息详情（包括采购单信息）</returns>
        public DataTable GetMessageOrderDetailBySubsidy(string id, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageOrderDetailBySubsidy(id, currentpage, pagesize, out totalrecord);
        }
        /// <summary>
        /// 查询预警信息详情(处方单预警)
        /// </summary>
        /// <param name="id">预警信息编号</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页最大数</param>
        /// <param name="totalrecord"></param>
        /// <returns>预警信息详情（包括采购单信息）</returns>
        public DataTable GetMessageOrderDetailBySubsidy(string where, string having, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageOrderDetailBySubsidy(where, having, currentpage, pagesize, out totalrecord);
        }

        public DataTable GetMessagePrescriptionDetail(string pid, int currentpage, int pagesize, out int totalrecord, out Model.Prescription p)
        {
            return DAL_MessageTypeDetail.GetMessagePrescriptionDetail(pid, currentpage, pagesize, out totalrecord, out p);
        }
        /// <summary>
        /// 根据id删除预警
        /// </summary>
        /// <param name="messageTypeDetailId"></param>
        /// <returns></returns>
        public int DeleteMessageTypeDetail(string messageTypeDetailId)
        {
            return DAL_MessageTypeDetail.DeleteMessageTypeDetail(messageTypeDetailId);
        }
        /// <summary>
        /// 根据id删除消息
        /// </summary>
        /// <param name="messageTypeDetailId"></param>
        /// <returns>0失败 1成功</returns>
        public int DeleteMessage(string id)
        {
            return DAL_MessageTypeDetail.DeleteMessage(id);
        }
        /// <summary>
        /// 预警开关（启用禁用）
        /// </summary>
        /// <param name="messageDetailId"></param>
        /// <returns></returns>
        public int WarningSwitch(string messageDetailId, string isWork)
        {
            return DAL_MessageTypeDetail.WarningSwitch(messageDetailId, isWork);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="messTypeDetail">model</param>
        /// <returns>1成功，0失败</returns>
        public int UpdateMessageTypeDetail(Model.MessageTypeDetail messTypeDetail)
        {
            return DAL_MessageTypeDetail.UpdateMessageTypeDetail(messTypeDetail);
        }
        /// <summary>
        /// 设置此条信息为已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int SetMessageIsRead(string id)
        {
            return DAL_MessageTypeDetail.SetMessageIsRead(id);
        }

        public DataTable GetMessageType()
        {
            return DAL_MessageTypeDetail.GetMessageType();
        }




        public void InsertIntoMessageSend()
        {
            //逻辑
            //1.查找规则表，有启用的规则，则2否返回
            //2.根据规则查找消息，有则3，否返回1继续循环
            //3.根据此消息的详细规则ID查找消息表中的非历史数据，有则4，无返5。
            //4.有非历史数据，判断是否在周期内，在6，不在7
            //5.插入此消息
            //6.返回3
            //7.将存在的消息置为历史消息，插入此消息
            DataTable dtMessTypeDetail = new DataTable();
            dtMessTypeDetail = DAL_MessageTypeDetail.GetMessDetailByWork();//取得已启用的规则信息

            DataTable dtTemp = new DataTable();

            //不存在返回
            if (dtMessTypeDetail.Rows.Count <= 0)
            {
                return;
            }
            else//存在根据规则查找信息
            {
                DAL_MessageTypeDetail.DelMessDetailByNoWork();
                //HZ.Data.DbHelperFactory.Create().ExecuteNonQuery("delete ");
                //循环规则详细
                for (int i = 0; i < dtMessTypeDetail.Rows.Count; i++)
                {
                    int TypeId = int.Parse(dtMessTypeDetail.Rows[i]["TypeId"].ToString());//规则类型编号
                    string _strHospitalId = dtMessTypeDetail.Rows[i]["HospitalId"].ToString();//医院编号
                    string _strGoodsId = dtMessTypeDetail.Rows[i]["GoodsId"].ToString();//药品编号
                    string _strCompanyId = dtMessTypeDetail.Rows[i]["CompanyId"].ToString();//企业编号
                    string _strDate = dtMessTypeDetail.Rows[i]["AddTime"].ToString();//规则添加时间
                    string _strDayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();//时间类型
                    string _strDays = dtMessTypeDetail.Rows[i]["Days"].ToString();//时间数量
                    string strsql = "";
                    List<Model.MessageSend> ListMess = new List<Model.MessageSend>();
                    switch (TypeId)
                    {
                        #region case1 医院药品监控
                        case 1://医院药品监控

                            //查出符合规则的消息
                            strsql = "select SUM(Amount) as Amount ,SUM(purchasecount) as Count from dbo.View_PurchaseOrderDetail where " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString() + "having" + dtMessTypeDetail.Rows[i]["SqlStringHaving"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            //判断此规则是否能产生对应消息
                            if (dtTemp.Rows.Count > 0)
                            {
                                //判断是否有历史数据
                                if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), "", "", "", ""))
                                {


                                    string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                    Model.MessageSend mess = new MessageSend();
                                    mess.Amount = decimal.Parse(dtTemp.Rows[0]["Amount"].ToString());
                                    mess.AreaId = dtMessTypeDetail.Rows[i]["AreaID"].ToString();
                                    // mess.AreaName = dtMessTypeDetail.Rows[i]["AreaName"].ToString();
                                    mess.BllId = "";
                                    mess.CompanyId_Jy = dtMessTypeDetail.Rows[i]["CompanyId"].ToString();
                                    mess.CompanyName_Jy = dtMessTypeDetail.Rows[i]["CompanyName"].ToString();
                                    mess.Content = "消息提示";
                                    mess.CreateTime = DateTime.Now;
                                    mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                    mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                    mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();
                                    mess.Is_Read = false;
                                    mess.HospitalId = dtMessTypeDetail.Rows[i]["HospitalID"].ToString();
                                    mess.HospitalName = dtMessTypeDetail.Rows[i]["HospitalName"].ToString();
                                    mess.Id = Id;
                                    mess.Is_Read = false;
                                    mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                    mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();
                                    mess.Numbers = int.Parse(dtTemp.Rows[0]["Count"].ToString());
                                    mess.ProductName = dtMessTypeDetail.Rows[i]["ProductName"].ToString();
                                    //mess.SortId = dtMessTypeDetail.Rows[i]["SortID"].ToString();
                                    mess.SortName = dtMessTypeDetail.Rows[i]["SortName"].ToString();
                                    mess.TableId = "OrderDetailID";
                                    mess.TableName = "View_PurchaseOrderDetail";
                                    mess.TypeId = 1;
                                    mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                    ListMess.Add(mess);
                                    //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                }
                                else
                                {
                                    //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                    if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                    {
                                        //将原来的消息放到历史数据库中，在此表中删除原来的数据 
                                        DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                        DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                        DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息

                                        string Id = Guid.NewGuid().ToString();
                                        //插入此消息
                                        Model.MessageSend mess = new MessageSend();
                                        mess.Amount = decimal.Parse(dtTemp.Rows[0]["Amount"].ToString());
                                        mess.AreaId = dtMessTypeDetail.Rows[i]["AreaID"].ToString();
                                        //mess.AreaName = dtMessTypeDetail.Rows[i]["AreaName"].ToString();
                                        mess.BllId = "";
                                        mess.CompanyId_Jy = dtMessTypeDetail.Rows[i]["CompanyId"].ToString();
                                        mess.CompanyName_Jy = dtMessTypeDetail.Rows[i]["CompanyName"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();

                                        mess.HospitalId = dtMessTypeDetail.Rows[i]["HospitalID"].ToString();
                                        mess.HospitalName = dtMessTypeDetail.Rows[i]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();
                                        mess.Numbers = int.Parse(dtTemp.Rows[0]["Count"].ToString());
                                        mess.ProductName = dtMessTypeDetail.Rows[i]["ProductName"].ToString();
                                        //mess.SortId = dtMessTypeDetail.Rows[i]["SortID"].ToString();
                                        mess.SortName = dtMessTypeDetail.Rows[i]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_PurchaseOrderDetail";
                                        mess.TypeId = 1;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        // DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region case3 医院已提交采购单企业未确认
                        case 3://医院已提交采购单企业未确认
                            //查出符合规则的消息
                            strsql = "select CompanyID_PS ,CompanyName_PS,OrderDetailID,AreaID,HospitalID,HospitalName,GoodsID,ProductName,SortName,SortSource  from dbo.View_PurchaseOrderDetail where OrderDetailState=1 and" + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            //判断此规则是否能产生对应消息
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID_PS"].ToString(), dtTemp.Rows[j]["GoodsID"].ToString(), dtTemp.Rows[j]["OrderDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtTemp.Rows[j]["GoodsID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                        //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_PurchaseOrderDetail";
                                        mess.TypeId = 3;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息置为历史消息
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除历史表里原有的数据
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息

                                            string Id = Guid.NewGuid().ToString();//不重复的主键

                                            //插入此消息
                                            Model.MessageSend mess = new MessageSend();
                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtTemp.Rows[j]["GoodsID"].ToString();
                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();
                                            mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                            //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "OrderDetailID";
                                            mess.TableName = "View_PurchaseOrderDetail";
                                            mess.TypeId = 3;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region case4 医院已提交退货单企业未确认
                        case 4://医院已提交退货单企业未确认
                            strsql = "select CompanyID,CompanyName,ReturnSerialDetailID,AreaID,HospitalID,HospitalName,ProcureCatalogID,ProductName,SortID,SortName,SortSource   from dbo.View_ReturnMerchandiseDetail where ReturnState=0 and " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID"].ToString(), dtTemp.Rows[j]["ProcureCatalogID"].ToString(), dtTemp.Rows[j]["ReturnSerialDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        // mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["ReturnSerialDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtTemp.Rows[j]["ProcureCatalogID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                        mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "ReturnSerialDetailID";
                                        mess.TableName = "View_ReturnMerchandiseDetail";
                                        mess.TypeId = 4;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息放到历史数据库中，在此表中删除原来的数据 
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息                                           

                                            Model.MessageSend mess = new MessageSend(); //插入此消息

                                            string Id = Guid.NewGuid().ToString();
                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            // mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["ReturnSerialDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtTemp.Rows[j]["ProcureCatalogID"].ToString();

                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                            mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                            //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "OrderDetailID";
                                            mess.TableName = "View_PurchaseOrderDetail";
                                            mess.TypeId = 4;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region case 6: 企业已确认订单医院未入库
                        case 6://企业已确认订单医院未入库
                            strsql = @"select CompanyID_PS ,CompanyName_PS,OrderDetailID,AreaID,
                            HospitalID,HospitalName,GoodsID,ProductName,SortName,SortSource  from dbo.View_PurchaseOrderDetail  where OrderDetailState=2 and WarehouseCount=0   and " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID_PS"].ToString(), dtTemp.Rows[j]["GoodsID"].ToString(), dtTemp.Rows[j]["OrderDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                        //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_PurchaseOrderDetail";
                                        mess.TypeId = 6;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息放到历史数据库中，在此表中删除原来的数据  
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息  
                                            string Id = Guid.NewGuid().ToString();
                                            //插入此消息
                                            Model.MessageSend mess = new MessageSend();

                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();

                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                            mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                            //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "OrderDetailID";
                                            mess.TableName = "View_PurchaseOrderDetail";
                                            mess.TypeId = 6;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region  case 7:医院已入库未回款
                        case 7://医院已入库未回款
                            strsql = "select CompanyID_PS ,CompanyName_PS,OrderDetailID,AreaID,HospitalID,HospitalName,GoodsID,ProductName,SortName,SortSource from dbo.View_PurchaseOrderDetail where WarehouseCount>0 and " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID_PS"].ToString(), dtTemp.Rows[j]["GoodsID"].ToString(), dtTemp.Rows[j]["OrderDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                        //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_PurchaseOrderDetail";
                                        mess.TypeId = 7;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息放到历史数据库中，在此表中删除原来的数据   
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息  
                                            string Id = Guid.NewGuid().ToString();
                                            //插入此消息
                                            Model.MessageSend mess = new MessageSend();

                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            // mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();

                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                            mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                            //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "OrderDetailID";
                                            mess.TableName = "View_PurchaseOrderDetail";
                                            mess.TypeId = 7;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region case 8 配送企业已确认采购单未发货
                        case 8:
                            strsql = "select CompanyID_PS ,CompanyName_PS,OrderDetailID,AreaID,HospitalID,HospitalName,GoodsID,ProductName,SortName,SortSource  from dbo.View_PurchaseOrderDetail  where OrderDetailState=2 and DistributeCount=0 and " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID_PS"].ToString(), dtTemp.Rows[j]["GoodsID"].ToString(), dtTemp.Rows[j]["OrderDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                        //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_PurchaseOrderDetail";
                                        mess.TypeId = 8;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        ///DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息放到历史数据库中，在此表中删除原来的数据  
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息  
                                            string Id = Guid.NewGuid().ToString();
                                            //插入此消息
                                            Model.MessageSend mess = new MessageSend();

                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            //mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();

                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                            mess.ProductName = dtTemp.Rows[j]["ProductName"].ToString();
                                            //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "View_PurchaseOrderDetail";
                                            mess.TableName = "OrderDetailID";
                                            mess.TypeId = 8;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion

                        #region case 9 配送企业已发货未开票
                        case 9:
                            strsql = "select CompanyID_PS ,CompanyName_PS,HospitalID,HospitalName ,AreaID,AreaName,GoodsID,GoodsName,OrderDetailID,SortName,SortSource from View_Distribution where IsInvoice=0 and DistributeCount>0 and " + dtMessTypeDetail.Rows[i]["SqlStringWhere"].ToString();
                            dtTemp = DAL_MessageTypeDetail.GetMessageSendByMessageTypeDetail(strsql);
                            ListMess.Clear();
                            if (dtTemp.Rows.Count > 0)
                            {
                                //循环插入
                                for (int j = 0; j < dtTemp.Rows.Count; j++)
                                {
                                    //判断是否有历史数据
                                    if (!DAL_MessageTypeDetail.IsExistMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString(), dtTemp.Rows[j]["HospitalID"].ToString(), dtTemp.Rows[j]["CompanyID_PS"].ToString(), dtTemp.Rows[j]["GoodsID"].ToString(), dtTemp.Rows[j]["OrderDetailID"].ToString()))
                                    {
                                        string Id = Guid.NewGuid().ToString(); //没有历史则插入根据规则查询的数据

                                        Model.MessageSend mess = new MessageSend();

                                        mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                        mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                        mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                        mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                        mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                        mess.Content = "消息提示";
                                        mess.CreateTime = DateTime.Now;
                                        mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                        mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                        mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();
                                        mess.Is_Read = false;
                                        mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                        mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                        mess.Id = Id;
                                        mess.Is_Read = false;
                                        mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                        mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                        mess.ProductName = dtTemp.Rows[j]["GoodsName"].ToString();
                                        //mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                        mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                        mess.TableId = "OrderDetailID";
                                        mess.TableName = "View_Distribution";
                                        mess.TypeId = 9;
                                        mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                        ListMess.Add(mess);
                                        //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                    }
                                    else
                                    {
                                        //比较(当前时间)与(规则制定时间与周期相加的时间和)
                                        if (DateTime.Compare(DateTime.Now, CountDays(_strDayType, int.Parse(_strDays), Convert.ToDateTime(_strDate))) > 0)//大于根据规则查找消息，且将原来的消息置为历史消息
                                        {
                                            //将原来的消息放到历史数据库中，在此表中删除原来的数据 
                                            DAL_MessageTypeDetail.DeleteMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());
                                            DAL_MessageTypeDetail.InsertIntoMessageSendHistory(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//将数据插入历史表
                                            DAL_MessageTypeDetail.DeleteMessageSend(dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString());//删除消息  
                                            string Id = Guid.NewGuid().ToString();
                                            //插入此消息
                                            Model.MessageSend mess = new MessageSend();

                                            mess.AreaId = dtTemp.Rows[j]["AreaID"].ToString();
                                            mess.AreaName = dtTemp.Rows[j]["AreaName"].ToString();
                                            mess.BllId = dtTemp.Rows[j]["OrderDetailID"].ToString();
                                            mess.CompanyId_Jy = dtTemp.Rows[j]["CompanyID_PS"].ToString();
                                            mess.CompanyName_Jy = dtTemp.Rows[j]["CompanyName_PS"].ToString();
                                            mess.Content = "消息提示";
                                            mess.CreateTime = DateTime.Now;
                                            mess.Days = int.Parse(dtMessTypeDetail.Rows[i]["Days"].ToString());
                                            mess.DayType = dtMessTypeDetail.Rows[i]["DayType"].ToString();
                                            mess.GoodsID = dtMessTypeDetail.Rows[i]["GoodsID"].ToString();

                                            mess.HospitalId = dtTemp.Rows[j]["HospitalID"].ToString();
                                            mess.HospitalName = dtTemp.Rows[j]["HospitalName"].ToString();
                                            mess.Id = Id;
                                            mess.Is_Read = false;
                                            mess.MessageDetailId = dtMessTypeDetail.Rows[i]["MessageDetailId"].ToString();
                                            mess.Name = dtMessTypeDetail.Rows[i]["TypeName"].ToString();

                                            mess.ProductName = dtTemp.Rows[j]["GoodsName"].ToString();
                                            mess.SortId = dtTemp.Rows[j]["SortID"].ToString();
                                            mess.SortName = dtTemp.Rows[j]["SortName"].ToString();
                                            mess.TableId = "OrderDetailID";
                                            mess.TableName = "View_Distribution";
                                            mess.TypeId = 9;
                                            mess.UserId = dtMessTypeDetail.Rows[i]["UserId"].ToString();
                                            ListMess.Add(mess);
                                            //DAL_MessageTypeDetail.InsertIntoMessageSend(mess);
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                    }
                                }
                                DAL_MessageTypeDetail.AddMessageSend_Bath(ListMess);
                            }
                            else
                            {
                                continue;
                            }
                            break;
                        #endregion
                    }
                }
            }
        }
        /// <summary>
        /// 规则制定时间和周期之和
        /// </summary>
        /// <param name="_strDayType">周期类型</param>
        /// <param name="_intDays">周期数量</param>
        /// <param name="date">规则制定时间</param>
        /// <returns>时间和</returns>
        public DateTime CountDays(string _strDayType, int _intDays, DateTime date)
        {
            DateTime addDate = DateTime.Now;
            switch (_strDayType)
            {
                case "日":
                    {
                        addDate = date.AddDays(_intDays);//日
                        break;

                    }
                case "周":
                    {
                        addDate = date.AddDays(7 * _intDays);//自然周7天
                        break;

                    }
                case "月":
                    {
                        addDate = date.AddMonths(_intDays);//自然月
                        break;

                    }
            }
            return addDate;

        }

        /// <summary>
        /// 消息是否存在
        /// </summary>
        /// <param name="_strWhere">查询条件</param>
        /// <returns>true存在， false不存在</returns>
        public bool IsExistMessageSend(string _strWhere)
        {
            return DAL_MessageTypeDetail.IsExistMessageSend(_strWhere);
        }

        /// <summary>
        /// 根据规则详细ID取规则查询条件
        /// </summary>
        /// <param name="_strMessageTypeDetailId">规则ID</param>
        /// <returns>SQLWHERE</returns>
        public string GetMessageTypeDeatilSqlStringWhere(string _strMessageTypeDetailId)
        {
            return DAL_MessageTypeDetail.GetMessageTypeDeatilSqlStringWhere(_strMessageTypeDetailId);
        }
        /// <summary>
        /// 根据规则详细ID取规则Having条件
        /// </summary>
        /// <param name="_strMessageTypeDetailId">规则ID</param>
        /// <returns>SQLWHERE</returns>
        public string GetMessageTypeDeatilSqlStringHaving(string _strMessageTypeDetailId)
        {
            return DAL_MessageTypeDetail.GetMessageTypeDeatilSqlStringHaving(_strMessageTypeDetailId);
        }
        public DataTable GetMessageType(Model.MessageTypeDetail messType, int IsRead, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessageType(messType, IsRead, currentpage, pagesize, out totalrecord);
        }
        public DataTable GetMessagSend(Model.MessageSend messSend, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessagSend(messSend, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 新添加的方法
        /// </summary>
        /// <param name="messSend"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public DataTable GetMessagSendNew(Model.MessageSend messSend, int isRead, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMessagSendNew(messSend, isRead, currentpage, pagesize, out totalrecord);
        }


        public DataTable GetDetailMessagSend(Model.MessageSend messSend, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetDetailMessagSend(messSend, currentpage, pagesize, out totalrecord);
        }


        public DataTable GetDetailMessagSendNew(Model.MessageSend messSend, int isRead, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetDetailMessagSendNew(messSend, isRead, currentpage, pagesize, out totalrecord);
        }
        public DataTable GetMessageForWindow()
        {
            return DAL_MessageTypeDetail.GetMessageForWindow();
        }

        /// <summary>
        /// 获取当前用户的全部预警消息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public DataTable GetMessagSendAll(string userid)
        {
            return DAL_MessageTypeDetail.GetMessagSendAll(userid);
        }

        #region 2011.4.9 赵洁 预警信息明细

        /// <summary>
        /// 配送企业已发货未开票详情
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType9(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType9(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 配送企业已确认采购单未发货
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType8(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType8(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 医院已提交企业未确认
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType3(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType3(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }



        /// <summary>
        /// 医院已入库未回款
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType7(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType7(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 配送企业已确认医院未入库
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType6(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType6(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 医院已提交退货单企业未确认
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType4(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType4(orderDetailId, h, a, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 医院已结算配送企业未确认
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetBllByType5(string orderDetailId, string h, string a, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetBllByType5(orderDetailId, h, a, currentpage, pagesize, out totalrecord);

        }

        /// <summary>
        /// 医药监控
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <returns></returns>
        public DataTable GetMedicinModel(string h,string orderName, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetMedicinModel(h,orderName, currentpage, pagesize, out totalrecord);

        }

        /// <summary>
        /// 药品监控，查询where条件
        /// </summary>
        /// <param name="isRead"></param>
        /// <returns></returns>
        public DataTable GetWhere(int isRead)
        {
            return DAL_MessageTypeDetail.GetWhere(isRead);
        }

        /// <summary>
        /// 药品监控，根据条件查询所有数据
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public DataTable GetAll(DataTable dt, int currentpage, int pagesize, out int totalrecord)
        {
            return DAL_MessageTypeDetail.GetAll(dt, currentpage, pagesize, out totalrecord);
        }
        #endregion
    }
}
